using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ebookreader.Resources;

namespace ebookreader
{
    public class PageSelPopUp : PopUp
    {
        #region Fields

        RenderableObject backgrounds, pop_up_box;
        //BoxedLabelButton back, go_to_page;
        VirtualKeyboard keyboard;
        TextureButton backspace;
        Reader reader;
        PopUp parent_popup;
        #endregion
        State currentState;
        public PageSelPopUp(Game game, Reader reader, PopUp parent_popup)
            : base(game)
        {
            this.reader = reader;
            this.parent_popup = parent_popup;
        }

        public override void Initialize()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            input_manager.OnTap += new InputManager.Tap(OnTap);
            currentState = Game.Services.GetService(typeof(State)) as State;

            input_manager.onBackButtonPressed += new InputManager.Back(() => { this.Dispose(); });

            base.Initialize();
        }


        Texture2D popup_read_body, popup_back, button_ring, back, key;
        SpriteFont font;
        VirtualKeyboard vk;
        protected override void LoadContent()
        {
            popup_read_body = Game.Content.Load<Texture2D>("Definitivo/page_popup_body");
            popup_back = Game.Content.Load<Texture2D>("todo/idle");
            button_ring = Game.Content.Load<Texture2D>("Definitivo/button_ring");
            back = Game.Content.Load<Texture2D>("Definitivo/black_back");
            key = Game.Content.Load<Texture2D>("Definitivo/key");
            font = Game.Content.Load<SpriteFont>("Fonts/menu_piccolo");

            var textedit_ring = Game.Content.Load<Texture2D>("Definitivo/text_edit");

            //backgrounds = new RenderableObject();
            //pop_up_box = new RenderableObject();

            //back = new BoxedLabelButton();
            //go_to_page = new BoxedLabelButton();
            //backspace = new TextureButton();

            //entries.AddRange(new MenuEntry[] { back, go_to_page, backspace });

            var textedit = new TextEdit(sprite_batch, font, textedit_ring, Color.Black, new Vector2(411, 105), "", 174, 42);
            vk = new VirtualKeyboard(Game, sprite_batch, font, 
                textedit,
                key, new string[] { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0","<-" }, 50, 50, 240, 160, 4, 6, 4, (s) => { }, input_manager);

            var confirm = new BoxedLabelButton(sprite_batch, button_ring, new Vector2(416, 330), font, Lang.Confirm, Color.Black, true, currentState.options);
            confirm.OnPress += new Press((me) =>
                {
                    try
                    {
                        reader.current_page = Math.Max(0, Math.Min(Int32.Parse(textedit.text)-1, Reader.pages.Count-1));
                    }
                    catch (Exception) { }
                    this.Dispose();
                    parent_popup.Dispose();
                });
            entries.Add(confirm);


            var back_button = new TextureButton(sprite_batch, back, new Vector2(162, 100), currentState.options);
            back_button.OnPress += new Press(
                (me) => { this.Dispose(); }
                );
            entries.Add(back_button);


            base.LoadContent();
        }


        protected override void Dispose(bool disposing)
        {
            vk.Dispose();
            base.Dispose(disposing);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }


        float time=1.0f, max_time=1.0f;
        Vector2 dest = new Vector2(107, 431);
        Vector2 now = new Vector2(225, 351);
        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();
            
            //sprite_batch.Draw(popup_back, GraphicsDevice.Viewport.Bounds, Color.White);
            sprite_batch.Draw(popup_read_body, new Vector2(142,80), Color.White);


            sprite_batch.DrawString(font, Lang.Go_to_page, new Vector2(395 - font.MeasureString(Lang.Go_to_page).X, 112), Color.Black);
            //if (dest.Y-now.Y > 1f)
            //{                
            //    now = Vector2.SmoothStep(now, dest, (float)gameTime.ElapsedGameTime.TotalSeconds);
            //    var rect = new Rectangle((int)now.X, 107, (int)now.Y, 267);
            //    sprite_batch.Draw(popup_read_body, rect, new Rectangle((int)(dest.Y- now.Y),0,(int)now.Y, 267), Color.White);
            //}
            //else
                



            sprite_batch.End();
            base.Draw(gameTime);
        }

    }
}
